unit clsNbm;

interface

uses Dm, dbtables, biblio;

type
  TNbm = class(TObject)
  private
    Nbm_codigo: string;
  public
    Nbm_desc: string;
    Nbm_descricao: string;
    function Gravar(p_Nbm_codigo: string): Byte;
    function Recuperar(p_Nbm_codigo: string): Byte;
    function Excluir(p_Nbm_codigo: string): Byte;
    procedure Consultar(var p_q: TQuery; p_desc: string; p_desc2: string = '');
  end;

implementation

function TNbm.Gravar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM Nbm where Nbm_codigo=:k_Nbm_codigo');
  tq.parambyname('k_Nbm_codigo').asstring := p_Nbm_codigo;
  tq.open;
  if tq.eof then begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('INSERT INTO Nbm VALUES(:k_Nbm_codigo,:k_Nbm_desc,:k_Nbm_descricao)');
  end else begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('UPDATE Nbm SET Nbm_desc=:k_Nbm_desc,Nbm_descricao=:k_Nbm_descricao WHERE Nbm_codigo=:k_Nbm_codigo');
  end;
  tq.parambyname('k_Nbm_codigo').asstring := p_Nbm_codigo;
  tq.parambyname('k_Nbm_desc').asstring := Nbm_desc;
  tq.parambyname('k_Nbm_descricao').asstring := Nbm_descricao;
  tq.close;
  tq.execsql;
  result := 1;
  tq.close;
  tq.Free;
end;

function TNbm.Recuperar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM Nbm where Nbm_codigo=:k_Nbm_codigo');
  tq.Params[0].asstring := p_Nbm_codigo;
  tq.open;
  Nbm_codigo := p_Nbm_codigo;
  if tq.eof then begin
    Nbm_desc := '';
    Nbm_descricao := '';
    Result := 0;
  end else begin
    Nbm_desc := tq.fieldbyname('Nbm_desc').asstring;
    Nbm_descricao := tq.fieldbyname('Nbm_descricao').asstring;
    Result := 1;
  end;
  tq.Close;
  tq.Free;
end;

function TNbm.Excluir;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM Nbm WHERE Nbm_codigo=:k_Nbm_codigo');
  tq.parambyname('k_Nbm_codigo').asstring := p_Nbm_codigo;
  tq.execSQL;
  Result := 1;
  tq.close;
  tq.Free;
end;

procedure TNbm.Consultar;
begin
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add('Select * from Nbm ');
  if p_desc2 = '' then begin
    p_q.sql.add('where Nbm_desc>=:k_Nbm_desc order by Nbm_desc');
    p_q.parambyname('k_Nbm_desc').asstring := p_desc;
  end else begin
    p_q.sql.add('where Nbm_descricao>=:k_Nbm_desc order by Nbm_descricao');
    p_q.parambyname('k_Nbm_desc').asstring := p_desc2;
  end;

end;

end.

